import { remove, getList, save } from '@/api/maint/msgreminder'
import permission from '@/directive/permission/index.js'

export default {
  directives: { permission },
  data() {
    return {
      editFormVisible: false,
      editFormTitle: '编辑消息',


      addFormVisible: false,
      addFormTitle: '添加消息',
      deptList: [],
      roleList: [],
      isAdd: true,
      checkedPermissionKeys: [],
      permissons: [],
      defaultProps: {
        id: 'id',
        label: 'name',
        children: 'children'
      },
      permissonVisible: false,
      addForm: {
        id: '',
        name: '',
        pId: null,
        reminderName: '',
        isPid: true,
        isDict: false,
        isTemplate: false,
        dictName: '',
        dictVaule: '',
        templateContent: '',
        description:'',
      },
      editForm: {
        id: '',
        name: '',
        pId: null,
        reminderName: '',
        description:'',
        isPid: true,
        isDict: false,
        isTemplate: false,
        // dictName:'',
        // dictVaule:'',
        // templateContent:'',
        dictChildren: [],
        templateChildren: [],

      },
      addFormRules: {
        name: [
          { required: true, message: '请输入消息名称', trigger: 'blur' },
        ],
        reminderName: [
          { required: true, message: '请输入消息参数', trigger: 'blur' },
        ],

      },
      editFormRules: {
        name: [
          { required: true, message: '请输入消息名称', trigger: 'blur' },
        ],
        reminderName: [
          { required: true, message: '请输入消息参数', trigger: 'blur' },
        ],

      },
      listQuery: {
        pageIndex: 1,
        pageSize: 10,
        name: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      allInvoices: {},
      allCurrentState: {},
      sort: { prop: 'name', order: 'descending' }
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(data => {
        this.list = data.data
        this.listLoading = false
        this.total = data.totalCount
      })
    },
    search() {
      this.fetchData()
    },
    reset() {
      this.listQuery.name = ''
      this.fetchData()
    },
    handleFilter() {
      this.getList()
    },
    fetchNext() {
      this.listQuery.pageIndex = this.listQuery.pageIndex + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.pageIndex = this.listQuery.pageIndex - 1
      this.fetchData()
    },
    fetchPage(pageIndex) {
      this.listQuery.pageIndex = pageIndex
      this.fetchData()
    },
    changeSize(pageSize) {
      this.listQuery.pageSize = pageSize
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
      //console.log(currentRow)
    },

    resetForm() {

      this.addForm = {
        id: '',
        name: '',
        pId: null,
        reminderName: '',
        isPid: true,
        isDict: false,
        isTemplate: false,
        dictName: '',
        dictVaule: '',
        templateContent: '',
        description:'',
      },
        this.editForm = {
          id: '',
          name: '',
          pId: null,
          reminderName: '',
          isPid: true,
          isDict: false,
          isTemplate: false,
          description:'',
          // dictName:'',
          // dictVaule:'',
          // templateContent:'',
          dictChildren: [],
          templateChildren: [],

        }
    },

    addFrom() {
      this.resetForm()
      this.addFormVisible = true
      this.isAdd = true
    },
    addDictDetail() {
      console.log(this.editForm)
      var details = this.editForm.dictChildren
      details.push({

        name: '',
        pId: null,
        reminderName: '',
        isPid: false,
        isDict: true,
        isTemplate: false,
        dictName: '',
        dictVaule: '',
        templateContent: null,
        id: null,
        description:'',
        key: Date.now()
      })
      this.editForm.dictChildren = details

    },
    addTemplateDetail() {

      var details = this.editForm.templateChildren
      details.push({

        name: '',
        pId: null,
        reminderName: '',
        isPid: false,
        isDict: false,
        isTemplate: true,
        dictName: null,
        dictVaule: null,
        templateContent: '',
        id: null,
        key: Date.now()
      })
      this.editForm.templateChildren = details

    },
    addSave() {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          console.log(this.addForm)
          save({
            id: this.addForm.id,
            name: this.addForm.name,
            pId: this.addForm.pId,
            isPid: this.addForm.isPid,
            reminderName: this.addForm.reminderName,
            isDict: this.addForm.isDict,
            isTemplate: this.addForm.isTemplate,
            // dictName:this.addForm.dictName,
            // dictVaule:this.addForm.dictVaule,
            // templateContent:this.addForm.templateContent,
            description: this.addForm.description,
          }).then(response => {
            this.$message({
              message: '提交成功',
              type: 'success'
            })
            this.fetchData()
            this.addFormVisible = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })

    },
    editSave() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          save({
            id: this.editForm.id,
            name: this.editForm.name,
            pId: this.editForm.pId,
            isPid: this.editForm.isPid,
            reminderName: this.editForm.reminderName,
            isDict: this.editForm.isDict,
            isTemplate: this.editForm.isTemplate,
            // dictName:this.addForm.dictName,
            // dictVaule:this.addForm.dictVaule,
            // templateContent:this.addForm.templateContent,
            description: this.editForm.description,
            dictChildren: this.editForm.dictChildren,
            templateChildren: this.editForm.templateChildren,
          }).then(response => {
            this.$message({
              message: '提交成功',
              type: 'success'
            })
            this.fetchData()
            this.editFormVisible = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })

    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: '请选中操作项',
        type: 'warning'
      })
      return false
    },
    Cancel() {
      this.formVisible = false
      this.resetForm()

    },
    edit(row) {
      console.log(row)
      this.resetForm()

      this.handleCurrentChange(row)

      if (this.checkSel()) {
        this.isAdd = false
        // this.editForm = this.selRow
        this.editForm.id = row.id
        this.editForm.name = row.name
        this.editForm.pId = row.pId
        this.editForm.reminderName = row.reminderName
        this.editForm.isDict = row.isDict
        this.editForm.isPid = row.isPid
        this.editForm.isTemplate = row.isTemplate
        this.editForm.description = row.description
        // this.editForm.dictChildren=row.dictChildren
        // this.editForm.templateChildren=row.templateChildren
        var details = [];
        var detailsMonth = []
        row.dictChildren.forEach(res => {
          details.push({
            name: res.name,
            pId: res.pId,
            reminderName: res.reminderName,
            isPid: res.isPid,
            isDict: res.isDict,
            isTemplate: res.isTemplate,
            dictName: res.dictName,
            dictVaule: res.dictVaule,
            templateContent: res.templateContent,
            description:res.description,
            id: res.id,
            key: Date.now()

          })
          // detailsMonth.push(res)
        })
        row.templateChildren.forEach(res => {
          detailsMonth.push({
            name: res.name,
            pId: res.pId,
            reminderName: res.reminderName,
            isPid: res.isPid,
            isDict: res.isDict,
            isTemplate: res.isTemplate,
            dictName: res.dictName,
            dictVaule: res.dictVaule,
            templateContent: res.templateContent,
            id: res.id,
            key: Date.now()

          })
          // detailsMonth.push(res)
        })

        this.editFormTitle = '修改账号'
        //this.form.disabledPwd=true
        this.editFormVisible = true
      }
      this.editForm.dictChildren = details
      this.editForm.templateChildren = detailsMonth
    },
    remove(row) {
      this.handleCurrentChange(row)
      if (this.checkSel()) {
        const id = row.id
        this.$confirm('确定删除消息以及参数和模板?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          remove(id).then(response => {
            this.$message({
              message: '提交成功',
              type: 'success'
            })
            this.fetchData()
          }).catch(err => {
            this.$notify.error({
              title: '错误',
              message: err
            })
          })
        }).catch(() => {
        })
      }
    },
    removeTemplateDetail(row) {
     
      if (row.pId == null) {
        var details = this.editForm.templateChildren
        details.forEach(function (val, index) {
          if (row.key === val.key) {
            details.splice(index, 1)
          }
        })
        this.editForm.templateChildren = details
      } else {
        if (this.checkSel()) {
          const id = row.id
          this.$confirm('确定删除该模板?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            var details =  this.editForm.templateChildren
            remove(id).then(response => {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              details.forEach(function (val, index) {
                if (row.id === val.id) {
                  details.splice(index, 1)
                }
              })
              this.editForm.templateChildren = details
              //this.formVisible = false
              this.fetchData()
            }).catch(err => {
              this.$notify.error({
                title: '错误',
                message: err
              })
              this.fetchData()
            })
          }).catch(() => {
          })
        }
      }
    },
    removeDictDetail(row) {
     
      if (row.pId == null) {
        var details = this.editForm.dictChildren
        details.forEach(function (val, index) {
          if (row.key === val.key) {
            details.splice(index, 1)
          }
        })
        this.editForm.dictChildren = details
      } else {
        if (this.checkSel()) {
          const id = row.id
          this.$confirm('确定删除该模板?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            var details =  this.editForm.dictChildren
            remove(id).then(response => {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              details.forEach(function (val, index) {
                if (row.id === val.id) {
                  details.splice(index, 1)
                }
              })
              this.editForm.dictChildren = details
              //this.formVisible = false
              this.fetchData()
            }).catch(err => {
              this.$notify.error({
                title: '错误',
                message: err
              })
              this.fetchData()
            })
          }).catch(() => {
          })
        }
      }
    }
  }
}
